/**
 * 
 */
package vn.edu.ctu.cit.se.android.karfinder;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author Darren
 *
 */
public class PlaylistMultiDeleteActivity extends Activity{

	private DatabaseHelper dbHelper;
	private ListView listView;
	private ArrayList<Playlist> playlistData;
	private Button btnDelete;
	private Button btnCancel;
	private ArrayAdapter<String> playlistAdapter;
	//private PlaylistAdapter playlistAdapter;	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_playlist_delete);
		
		
		dbHelper = new DatabaseHelper(this);
		playlistData = dbHelper.fetchPlaylistList();
		listView = (ListView)findViewById(R.id.lvPlaylistDelete);	
		//playlistAdapter = new PlaylistAdapter();
		String[] arrayPlaylist = new String[playlistData.size()];
		for(int i=0; i < playlistData.size(); i++){
			arrayPlaylist[i]=playlistData.get(i).getName();
		}
		playlistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arrayPlaylist);
		listView.setAdapter(playlistAdapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		btnDelete = (Button)findViewById(R.id.btnPlaylistDelete);
		btnCancel = (Button)findViewById(R.id.btnPlaylistCancel);	
		btnDelete.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {			
				AlertDialog.Builder dialogDeleteMultiPlaylist = new AlertDialog.Builder(PlaylistMultiDeleteActivity.this);
				dialogDeleteMultiPlaylist.setTitle(R.string.dialog_delete_multiplaylist);
				dialogDeleteMultiPlaylist.setMessage(R.string.message_delete_multiplaylist);
				dialogDeleteMultiPlaylist.setPositiveButton(R.string.dialog_button_playlist_ok, 
									new DialogInterface.OnClickListener() {											
										public void onClick(DialogInterface dialog, int which) {																			
											long[] arrayChecked = listView.getCheckItemIds();				
											for(int i=0; i<arrayChecked.length; i++){
												Playlist playlist = new Playlist();
												playlist.setId(playlistData.get((int)arrayChecked[i]).getId());
												dbHelper.deletePlaylist(playlist);
											}								
											Toast.makeText(PlaylistMultiDeleteActivity.this, R.string.dialog_delete_playlist_successful, Toast.LENGTH_SHORT).show();
											resetList();																
										}
									});
				dialogDeleteMultiPlaylist.setNegativeButton(R.string.dialog_button_playlist_cancel, 
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {									
							}
						});
				dialogDeleteMultiPlaylist.show();	
			}			
		});
		
		btnCancel.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				finish();				
			}
			
		});
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dbHelper.close();
	}



	public void resetList(){		
		playlistData = dbHelper.fetchPlaylistList();		
		String[] arrayPlaylist = new String[playlistData.size()];
		for(int i=0; i < playlistData.size(); i++){
			arrayPlaylist[i]=playlistData.get(i).getName();
		}
		playlistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arrayPlaylist);
		//playlistAdapter.notifyDataSetChanged();
		listView.setAdapter(playlistAdapter);
	}
	

}